package com.tmt.datatable;

import com.tmt.datatable.model.FieldOption;
import com.tmt.datatable.model.FileWrapper;
import org.springframework.data.domain.Page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * dataTable 数据响应
 * Created by Stark on 2017/3/11.
 */
public class TableResponse<T extends Serializable> implements Serializable {

	private long recordsTotal;
	private long recordsFiltered;
	private int draw;
	private List<T> data = new ArrayList<>();
	private FileWrapper files = null;
	private Map<String, List<FieldOption>> options = new LinkedHashMap<>();

	private TableRequest request;

	public long getRecordsTotal() {
		return recordsTotal;
	}

	public void setRecordsTotal(long recordsTotal) {
		this.recordsTotal = recordsTotal;
	}

	public long getRecordsFiltered() {
		return recordsFiltered;
	}

	public void setRecordsFiltered(long recordsFiltered) {
		this.recordsFiltered = recordsFiltered;
	}

	public int getDraw() {
		return draw;
	}

	public void setDraw(int draw) {
		this.draw = draw;
	}

	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	public FileWrapper getFiles() {
		return files;
	}

	public void setFiles(FileWrapper files) {
		this.files = files;
	}

	public Map<String, List<FieldOption>> getOptions() {
		return options;
	}

	public void setOptions(Map<String, List<FieldOption>> options) {
		this.options = options;
	}

	public TableRequest getRequest() {
		return request;
	}

	public void setRequest(TableRequest request) {
		this.request = request;
	}

	public TableResponse(List<T> data) {
		this(data, data.size(), null);
	}

	public TableResponse(List<T> data, long count, TableRequest request) {
		this(data, count, count, request);
	}

	public TableResponse(List<T> data, long totalCount, long filteredCount, TableRequest request) {
		this(data, totalCount, filteredCount, request, null);
	}

	public TableResponse(List<T> data, long totalCount, long filteredCount, TableRequest request, FileWrapper files) {
		this.data = data;
		this.recordsTotal = totalCount;
		this.recordsFiltered = filteredCount;
		this.request = request;
		this.files = files;
		this.draw = request == null ? 0 : request.getDraw() + 1;
	}

	public static <T extends Serializable> TableResponse<T> fromPage(Page<T> page) {
		return new TableResponse<>(page.getContent(), page.getTotalElements(), page.getTotalElements(), null);
	}


	/**
	 * 空白的表格数据响应
	 */
	public static <T extends Serializable> TableResponse<T> empty() {
		return new TableResponse<>(new ArrayList<>());
	}
}
